package project_day4;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.xml.parsers.FactoryConfigurationError;

public class MainApp {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		
		String path = "G:\\logs\\log.log";
		File file = new File(path);

		if (file.exists() && file.isFile()) {
			// main
			FileInputStream fileInputs = null;
			try {

				FileInputStream fileinputs = new FileInputStream(file);
				
				InputStreamReader InputStreamReader = new InputStreamReader(fileinputs);
				BufferedReader bufferedReader = new BufferedReader(InputStreamReader);
				//bufferedReader.readLine();
				//System.out.println(bufferedReader.readLine());
				String line = null;
				//D:/log\ips.txt
                File ipFile = new File("G:\\logs\\ips1.txt");

                if(!ipFile.exists()){
                	
                	ipFile.createNewFile();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(ipFile);
				BufferedOutputStream  bufferedOutputStream  = new BufferedOutputStream  (fileOutputStream);
				while((line = bufferedReader.readLine()) != null){

                   // System.out.println(MainApp.getIP(line));
                   // FileOutputStream fileOutputStream = new FileOutputStream(ipfile);
                    //BufferedOutputStream bufferedOutputStream = new BufferedOutputStream
                    fileOutputStream.write(MainApp.getIP(line).getBytes());
                    fileOutputStream.write("\n".getBytes());
                }
				fileOutputStream.flush();
				fileOutputStream.close();
			} catch (FileNotFoundException e) {
				e.printStackTrace();
				return;
			}catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		} else {
			System.out.println("not a file or not exist!!");
			
		}
		System.out.println("end");
	}
		
	public static String getIP(String content){
			String regUrl = "((?:(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d)))\\.){3}(?:25[0-5]|2[0-4]\\d|((1\\d{2})|([1-9]?\\d))))";
	        
			Pattern p = Pattern.compile(regUrl, Pattern.CASE_INSENSITIVE);
	       
			Matcher m = p.matcher(content);
	        boolean blnp = m.find();
	       if(blnp = true){
	    	   return m.group();
	       }

	       return null;
		}

}

